﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System.Collections.Generic;
using NUnitGrid.Server.Plugins.Clients.Client;
using NUnitGrid.Server.Plugins.Nodes;
using NUnitGrid.Server.Plugins.Nodes.Node;


namespace NUnitGrid.Server.Plugins.Clients
{
    public class GridClients : IGridClients, IGridNodeListener<IGridNodeMetrics>, INodeAvailableListener<IClientProxy>,
                               IGridMetricsListener
    {
        private readonly List<IClientProxy> allClients = new List<IClientProxy>();
        private readonly IClientProxyFactory clientFactory;
        private readonly IGridNodesListener listener;
        private readonly List<IClientProxy> onlineClients = new List<IClientProxy>();

        public GridClients(IGridNodesListener listener, IClientProxyFactory clientFactory)
        {
            this.listener = listener;
            this.clientFactory = clientFactory;
        }

        public int Count
        {
            get { return allClients.Count; }
        }

        public void Start()
        {
            for (var clientCount = 0; clientCount < GridServerSettings.Default.MaximumNumberOfClients; clientCount++)
            {
                OpenListeningLink();
            }
        }

        public void Stop()
        {
            foreach (var node in allClients)
            {
                node.Stop();
            }
        }

        void IGridMetricsListener.OnMetricsUpdate(IGridMetrics gridMetrics)
        {
            foreach (var client in onlineClients)
            {
                client.OnMetricsUpdate(gridMetrics);
            }
        }

        void IGridNodeListener<IGridNodeMetrics>.OnChange(IGridNodeMetrics gridNode)
        {
            listener.OnNodesChanged(onlineClients.ToArray());
        }

        void INodeAvailableListener<IClientProxy>.OnNodeAvailable(IClientProxy node, NodeAvailability availability)
        {
            if (availability == NodeAvailability.Available)
            {
                onlineClients.Add(node);
            }
            else
            {
                if (onlineClients.Contains(node))
                {
                    onlineClients.Remove(node);
                }
            }
            listener.OnNodesChanged(onlineClients.ToArray());
        }

        private void OpenListeningLink()
        {
            var node = clientFactory.Create();
            allClients.Add(node);
            node.Start();
        }
    }
}